package com.kay.service.impl;


import com.kay.annote.Autowired;
import com.kay.annote.Service;
import com.kay.annote.Transactional;
import com.kay.dao.AccountDao;
import com.kay.pojo.Account;
import com.kay.service.TransferService;

@Transactional
@Service(value = "transferService")
public class TransferServiceImpl implements TransferService {

    @Autowired
    private AccountDao accountDao;

//    public void setAccountDao(AccountDao accountDao) {
//        this.accountDao = accountDao;
//    }

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        try {
            // 开启事务即：设置不自动提交
//            TransactionManager.getInstance().beginTransaction();
            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);

            from.setMoney(from.getMoney() - money);
            to.setMoney(to.getMoney() + money);

            accountDao.updateAccountByCardNo(to);
            int i = 1 / 0; // 制造异常
            accountDao.updateAccountByCardNo(from);
            // 提交
//            TransactionManager.getInstance().commitTransaction();
        }catch (Exception e){
            e.printStackTrace();
            // 异常回滚
//            TransactionManager.getInstance().rollBack();
            throw e;
        }
    }
}
